package cn.ven.controller;

import cn.ven.entity.*;
import cn.ven.service.BlogService;
import cn.ven.service.TypeService;
import cn.ven.service.UserService;
import cn.ven.tool.RespJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/public")
public class PublicController {
    private Logger logger =LoggerFactory.getLogger(PublicController.class);
    @Autowired
    UserService userService;

    @Autowired
    BlogService blogService;

    @Autowired
    TypeService typeService;

    @RequestMapping("/go2Login")
    public String go2Login(){
        return "index";
    }

    @RequestMapping("/goRegister")
    public String goRegister(){
        return "login/register";
    }

    @RequestMapping("/goLogin")
    public String goLogin(){
        return "login/login";
    }

    @RequestMapping("/go2Content")
    public String go2Content(){
        return "content/content";
    }

    //注册用户
    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String,Object> addUser(@RequestBody MyUser user){
        MyUser myUser = userService.findByUsername(user.getUsername());
        if (myUser==null){
            userService.addUser(user);
            return RespJson.pack(200,"注册成功","");
        }
       return RespJson.pack(500,"该用户已存在，注册失败","");
    }

    //首页输出博客摘要，标题，作者（user.nickname）
    @RequestMapping("/findAllBlog")
    @ResponseBody
    public Map<String,Object> findAllBlog(){
        List<Blog> blogs = blogService.findAll(1);
        logger.debug("blogs===="+blogs);
        System.out.println("blogs==="+blogs);
        return RespJson.pack(200,"",blogs);
    }


    //去主页
    @RequestMapping("/go2Index")
    public String go2Index(){
        return "index";
    }

    //查找所有的类型
    @RequestMapping("/findAllType")
    @ResponseBody
    public Map<String,Object> findAll(){
        List<Type> types = typeService.findAll();
        logger.debug("types==="+types);
        return RespJson.pack(200,"查找成功",types);
    }

    //根据id查找对应博客信息
    @RequestMapping("/findBlogTypeContent/{id}")
    @ResponseBody
    public Map<String,Object> findBlogTypeContent(@PathVariable("id")String id){
        BlogTypeContent blogTypeContent = blogService.findBlogTypeContent(id);
        logger.debug("blogTypeContent====="+blogTypeContent);
        return RespJson.pack(200,"",blogTypeContent);
    }

    //游览量加一
    @RequestMapping("/addReading_num/{id}")
    @ResponseBody
    public Map<String,Object> addReading_num(@PathVariable("id")String id){
        System.out.println("id===="+id);
        blogService.updateReading_mum(id);
        return RespJson.pack(200,"阅读量加一","");
    }


    //根据类型id查对应的博客
    @RequestMapping("/findBlogInType/{id}")
    @ResponseBody
    public Map<String,Object> findTypeBlogContents(@PathVariable("id")String id){
        Type type = typeService.findById(id);
        List<Blog> blogs = blogService.findBlogByTypeid(type.getId());
        return RespJson.pack(200,"",blogs);
    }




}
